package com.swq.java.code.chapter13.code15;

import java.util.Timer;
import java.util.TimerTask;

public class Machine extends Thread {
    private int a;

    public void start() {
        super.start();
        Timer timer = new Timer(true);  //把与Timer关联的线程设为后台线程

        TimerTask task = new TimerTask() {  //匿名类
            public void run() {
                reset();
            }
        };

        timer.schedule(task, 10, 50);  //设置定时任务
    }

    public void reset() {
        a = 0;
    }

    public void run() {
        for (int i = 0; i < 1000; i++) {
            System.out.println(getName() + ":" + a++);
            yield();
        }
    }

    public static void main(String args[]) throws Exception {
        Machine machine = new Machine();
        machine.start();
    }
}
